public class TesTheap {
    public int[] elem;
    public int usedSize;
    public TesTheap(){
        this.elem=new int [10];
    }
    public void init(int[] array){
        for (int i = 0; i < array.length; i++) {
            elem[i]=array[i];
            usedSize++;
        }
    }
    //从最后一棵子树开始调整
    //左右孩子最大值和根节点比较,如果大于根节点交换
    public void createHeap(){
        for (int parent = (usedSize-1-1)/2; parent >=0 ; parent--) {
             siftDown(parent,usedSize);
        }
    }
    private void swap(int i,int j){
        int temp=elem[i];
        elem[i]=elem[j];
        elem[i]=temp;
    }
    public void siftDown(int parent,int end){
        int child=2*parent+1;
        while(child<end){
            if (child+1<end && elem[child] < elem[child+1]){
                child++;
            }
            if(elem[child]>elem[parent]){
                swap(child,parent);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }

}
